library AmmoReload initializer init requires LegacySystem, AmmoDatabase, AmmoAPI, DeltaChaingun


	private function HSReloadRemaining takes player p returns nothing
		local integer i = GetConvertedPlayerId(p)
		set AmmoSystemRounds[i] = 100*AmmoSystemMagazines[i]
		call ShiftMoveSpeed(udg_Hero[i],1.4*I2R(AmmoSystemMagazines[i]))
		set AmmoSystemMagazines[i] = 0
		call SetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD,AmmoSystemRounds[i])
		call SetPlayerState(p,PLAYER_STATE_RESOURCE_LUMBER,AmmoSystemMagazines[i])
	endfunction

	private function ReloadMagazine takes player p, integer mags returns nothing
		local integer i = GetConvertedPlayerId(p)
		set AmmoSystemMagazines[i] = AmmoSystemMagazines[i] - mags
		set AmmoSystemRounds[i] = GetUnitUserData(udg_Hero[i]) // user data = rounds per mag
		call ShiftMoveSpeed(udg_Hero[i],1.4*I2R(mags))
		call SetPlayerState(p,PLAYER_STATE_RESOURCE_GOLD,AmmoSystemRounds[i])
		call SetPlayerState(p,PLAYER_STATE_RESOURCE_LUMBER,AmmoSystemMagazines[i])
	endfunction
	
	private function AutoReloadConditions takes nothing returns boolean
		local player p = GetTriggerPlayer()
		local integer i = GetConvertedPlayerId(p)
		local integer r = udg_Reloading[i]
		return AmmoSystemMagazines[i]>=1 and GetWidgetLife(udg_Hero[i])>0.405 and AmmoSystemRounds[i]==0 and (r!=1 or r!=2) and GetUnitTypeId(udg_Hero[i])!='H000'
	endfunction

	private function AutoReload takes nothing returns nothing
		local player p = GetTriggerPlayer()
		local integer i = GetConvertedPlayerId(p)
		local integer i2
		local integer id = GetUnitTypeId(udg_Hero[i])
		local integer r = udg_Reloading[i]
		local real t = -0.65
		local integer current = udg_Second
		if udg_Reloading[i]==2 then
			return
		endif
		set udg_Reloading[i] = 1
		call UnitAddAbility(udg_Hero[i],'A02H')
		if current-udg_ClipOutLimit!=0 then
			set bj_meleeNearestMineDist = GetUnitX(udg_Hero[i])
			set bj_lastTransmissionDuration = GetUnitY(udg_Hero[i])
			set udg_soundpath = "war3mapImported\\clipout1.wav"
			call PlaySoundPath3D.execute()
		else
			set udg_ClipOutLimit=current
		endif
		if GetRandomInt(1,3)==1 and udg_Players>1 then
			call FieldChat(udg_Hero[i], 5.0, udg_Pcolors[i] + GetPlayerName(p) + "|r : Reloading." )
		endif
	   // if AilmentCheck(udg_Hero[i]) then
	   //     set t=t+2.45
	  //  endif
		if GetUnitAbilityLevel(udg_Hero[i],'B003')>2 then
			set t=t-0.2
		endif
		if id=='H006' then
			call UnitAddAbility(udg_Hero[i],'A07Y')
		elseif id!='H003' and id!='H008' and GetUnitAbilityLevel(udg_Hero[i],'A07V')<3 and GetUnitAbilityLevel(udg_Hero[i],'A03Q')<3 then
			call UnitAddAbility(udg_Hero[i],'A07X')
		endif
		call PolledWait(udg_ReloadTime[i]+t)
		set current = udg_Second
		if current-udg_ClipInLimit!=0 then
			set bj_meleeNearestMineDist = GetUnitX(udg_Hero[i])
			set bj_lastTransmissionDuration = GetUnitY(udg_Hero[i])
			set udg_soundpath = "war3mapImported\\clipin1.wav"
			call PlaySoundPath3D.execute()
		else
			set udg_ClipInLimit=current
		endif
		call PolledWait(0.55)
		set bj_meleeNearestMineDist = GetUnitX(udg_Hero[i])
		set bj_lastTransmissionDuration = GetUnitY(udg_Hero[i])
		if id=='H007' then
			set udg_soundpath = "war3mapImported\\m3_pump.wav"
		elseif id=='Hpal' or id=='H00J' or id=='H003' then //Rifleman,IUL,Marksman
			set udg_soundpath = "war3mapImported\\m4a1_boltpull.wav"
		else
			set udg_soundpath = "war3mapImported\\Reload.wav"
		endif
		call PlaySoundPath3D.execute()
		call PolledWait(0.1)
		if udg_Hero[i]==null then
			return
		endif
		set i2 = GetUnitAbilityLevel(udg_Hero[i],'A00M')
		if AmmoSystemMagazines[i]<6 and GetUnitItem(udg_Hero[i],'shas')==null and GetUnitItem(udg_Hero[i],'I006')==null and GetRandomInt(1,3)==1 and udg_Players>1 then
			call FieldChat(udg_Hero[i], 5.0, udg_Pcolors[i] + GetPlayerName(p) + "|r : Running low on mags." )
		endif
		if AmmoSystemMagazines[i]<1+i2 then
			call HSReloadRemaining(p)
		else
			call ReloadMagazine(p,1+i2)
		endif
		if r==4 or IsUnitChaingunOperator(udg_Hero[i]) then
			set udg_Reloading[i] = 4
		elseif udg_CombatKnife[i] then
			set udg_Reloading[i] = 5
		else
			set udg_Reloading[i] = 0
		endif
		call UnitRemoveAbility(udg_Hero[i],'A02H')
		if id=='H006' then
			call UnitRemoveAbility(udg_Hero[i],'A07Y')
		elseif id!='H003' and id!='H008' then
			call UnitRemoveAbility(udg_Hero[i],'A07X')
		endif
	endfunction

	
	private function ManualReloadConditions takes nothing returns boolean
		local player p = GetTriggerPlayer()
		local integer i = GetConvertedPlayerId(p)
		local integer r = udg_Reloading[i]
		return GetSpellAbilityId()=='A007' and AmmoSystemMagazines[i]>=1 and (r!=1 or r!=2)
	endfunction

	private function ManualReload takes nothing returns nothing
		local player p = GetOwningPlayer(GetTriggerUnit())
		local integer i = GetConvertedPlayerId(p)
		local integer i2
		local integer id = GetUnitTypeId(udg_Hero[i])
		local integer r = udg_Reloading[i]
		local real t = -0.65
		if udg_Reloading[i]==1 or AmmoSystemRounds[i]==GetUnitUserData(udg_Hero[i]) then
			return
		endif
		set udg_Reloading[i] = 2
		call UnitAddAbility(udg_Hero[i],'A02H')
		set AmmoSystemRounds[i] = 0
		call SetPlayerState( p, PLAYER_STATE_RESOURCE_GOLD, AmmoSystemRounds[i] )
		call AttachSoundToUnit(gg_snd_clipout, udg_Hero[i])
		call SetSoundVolume(gg_snd_clipout, 127)
		call StartSound(gg_snd_clipout)
		if GetRandomInt(1,2)==1 and udg_Players>1 then
			call FieldChat(udg_Hero[i], 5.0, udg_Pcolors[i] + GetPlayerName(p) + "|r : Reloading." )
		endif
	   // if AilmentCheck(udg_Hero[i]) then
	   //     set t=t+2.45
	   // endif
		if GetUnitAbilityLevel(udg_Hero[i],'B003')>2 then
			set t=t-0.2
		endif
		if id=='H006' then
			call UnitAddAbility(udg_Hero[i],'A07Y')
		elseif id!='H003' and id!='H008' and GetUnitAbilityLevel(udg_Hero[i],'A08N')<3 and GetUnitAbilityLevel(udg_Hero[i],'A03Q')<3 then
			call UnitAddAbility(udg_Hero[i],'A07X')
		endif
		call PolledWait(udg_ReloadTime[i]+t)
		call AttachSoundToUnit(gg_snd_clipin, udg_Hero[i])
		call SetSoundVolume(gg_snd_clipin, 127)
		call StartSound(gg_snd_clipin)
		call PolledWait(0.55)
		set bj_meleeNearestMineDist = GetUnitX(udg_Hero[i])
		set bj_lastTransmissionDuration = GetUnitY(udg_Hero[i])
		if id=='H007' then
			set udg_soundpath = "war3mapImported\\m3_pump.wav"
		elseif id=='Hpal' or id=='H00J' or id=='H003' then //Rifleman,IUL,Marksman
			set udg_soundpath = "war3mapImported\\m4a1_boltpull.wav"
		else
			set udg_soundpath = "war3mapImported\\Reload.wav"
		endif
		call PlaySoundPath3D.execute()
		call PolledWait(0.1)
		if udg_Hero[i]==null then
			return
		endif
		if r==4 then
			set udg_Reloading[i] = 4
		elseif udg_CombatKnife[i] then
			set udg_Reloading[i] = 5
		else
			set udg_Reloading[i] = 0
		endif
		set i2 = GetUnitAbilityLevel(udg_Hero[i],'A00M')
		if AmmoSystemMagazines[i]==0 then
			set udg_Reloading[i] = 3
			return
		elseif AmmoSystemMagazines[i]<1+i2 then
			call HSReloadRemaining(p)
		else
			call ReloadMagazine(p,1+i2)
		endif
		call UnitRemoveAbility(udg_Hero[i],'A02H')
		if id=='H006' then
			call UnitRemoveAbility(udg_Hero[i],'A07Y')
		elseif id!='H003' and id!='H008' then
			call UnitRemoveAbility(udg_Hero[i],'A07X')
		endif
	endfunction



	private function init takes nothing returns nothing
		local trigger t
		set AmmoAutoReloadTrigger = CreateTrigger()
		call TriggerAddCondition(AmmoAutoReloadTrigger,Condition(function AutoReloadConditions))
		call TriggerAddAction(AmmoAutoReloadTrigger,function AutoReload)
		
		set t = CreateTrigger()
		call TriggerRegisterAnyUnitEventBJ( t,  EVENT_PLAYER_UNIT_SPELL_CAST  )
		call TriggerAddCondition( t, Condition( function ManualReloadConditions ) )
		call TriggerAddAction(t,function ManualReload)
	endfunction




endlibrary